package com.leyou.item.web;

import com.leyou.common.dto.BrandDTO;
import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.entity.Category;
import com.leyou.item.service.BrandService;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author 虎哥
 */
@RestController
@RequestMapping("/category")
public class CategoryController {

    private final CategoryService categoryService;

    public CategoryController(CategoryService categoryService) {
        this.categoryService = categoryService;
    }
     @RequestMapping("/{id}")                           //@PathVariable 路径占位符
    public ResponseEntity<CategoryDTO> QUERYcATEGORYbYiD(@PathVariable("id") long id){
         Category category = categoryService.getById(id);
         CategoryDTO categoryDTO = new CategoryDTO(category);
         return ResponseEntity.ok(categoryDTO);
        // return ResponseEntity.ok(new CategoryDTO(categoryService.getById(id)));
     }
    @GetMapping("list")                                       // @RequestParam请求类型是query
    public ResponseEntity<List<CategoryDTO>> queryCategoryByIds(@RequestParam("ids") List<Long> ids){
        //CategoryDTO.convertEntityList 是一个工具类
        return ResponseEntity.ok(CategoryDTO.convertEntityList(categoryService.listByIds(ids)));
    }
    /**
     * 根据父类目id查询子类目的集合
     * @param pid 父类目的id
     * @return 子类目的集合
     */
    @GetMapping("/of/parent")
    public ResponseEntity<List<CategoryDTO>> queryCategoryByParentId(@RequestParam("pid") Long pid){
        return ResponseEntity.ok(// 封装并返回
                CategoryDTO.convertEntityList(// 把PO集合转为DTO集合
                        categoryService.query().eq("parent_id", pid).list()// 根据父类目id查询集合
                )
        );
    }

}